var $tbody = $("#sample-table-1 tbody"),
    typeUrl;
if(sessionStorage.getItem('musicType') == '2aeded1129784decbab8b8bf18329c7c'){
    $('#deleteMusic,#exportUser').remove()
}
/**
 * 日期控件实例化
 */
jQuery(function ($) {
    $('.date-picker').datepicker({autoclose: true}).next().on(ace.click_event, function () {
        $(this).prev().focus();
    });
});
console.log("212121121");
// 初始化table
$("#musicList_table").bootstrapTable({
    'url': '/community/musictype/findTypeMusic',
    'cache':false,
    //'method': 'post',
    'queryParams':function(params){
        params["typeId"] = sessionStorage.getItem('musicType');
        params['limit'] = params.limit;
        params['offset'] = params.offset/params.limit + 1;
        return params;
    },
    'toolbar': $('div[data-name="btnCtl"]'),
    'striped': true,
    'pagination': true,
    'sidePagination': 'server',
    'pageSize': globalPageSize,
    'pageList': [],
    'clickToSelect': true,
    'singleSelect': false,
    'smartDisplay': true,
    'responseHandler': function(resb) {
    	return {
    		'total': resb.data.total,
    		'rows': resb.data.rows
    	}
    },
    'onCheckAll':function(){
        if(sessionStorage.getItem('musicType') == '2aeded1129784decbab8b8bf18329c7c'){
            $("#copyUser").removeAttr("disabled");
        }else{
            $("#deleteMusic,#copyUser").removeAttr("disabled");
        }
    },
    'onUncheckAll': function(){
        $("#deleteMusic,#copyUser").attr("disabled", 'true');
    },
    'onCheck': function() {
        if(sessionStorage.getItem('musicType') == '2aeded1129784decbab8b8bf18329c7c'){
            $("#copyUser").removeAttr("disabled");
        }else{
            $("#deleteMusic,#copyUser").removeAttr("disabled");
        }
    },
    'onUncheck': function() {
        $("#deleteMusic,#copyUser").attr("disabled", 'true');
    },
    'columns': [{
        'checkbox': true
        //'class': 'hide'
    }, {
        'field': '',
        'title': '排序',
        'align': 'center',
        'formatter':function (value,row,index) {
            index+=1;
            return index;
        }
    }, {
        'field': 'musicName',
        'title': '音乐名',
        'align': 'center'
    }, {
        'field': 'singer',
        'title': '歌手名',
        'align': 'center'
    }, {
        'field': 'playCount',
        'title': '使用次数',
        'align': 'center'
    }]
});

$('#musicModal').on('show.bs.modal',function () {
    $('#music_table').bootstrapTable({
        'url': '/community/music/findMusicFromLibrary',
        'cache':false,
        //'method': 'post',
        'queryParams':function(params){
            params["typeId"] = sessionStorage.getItem('musicTypes');
            params['limit'] = params.limit;
            params['offset'] = params.offset/params.limit + 1;
            return params;
        },
        'toolbar': $('div[data-name="btnCtl"]'),
        'striped': true,
        'pagination': true,
        'sidePagination': 'server',
        'pageSize': globalPageSize,
        'pageList': [],
        'clickToSelect': true,
        'singleSelect': false,
        'smartDisplay': true,
        'responseHandler': function(resb) {
            return {
                'total': resb.data.total,
                'rows': resb.data.rows
            }
        },
        'onCheck': function() {
            $("button[data-target='#updateModal'], button[data-target='#deleteModal']").removeAttr("disabled");
        },
        'onUncheck': function() {
            $("button[data-target='#updateModal'], button[data-target='#deleteModal']").attr("disabled", 'true');
        },
        'columns': [{
            'checkbox': true
            //'class': 'hide'
        }, {
            'field': '',
            'title': '推荐排序',
            'align': 'center',
            'formatter':function (value,row,index) {
                index+=1;
                return index
            }
        }, {
            'field': 'musicName',
            'title': '音乐名',
            'align': 'center'
        }, {
            'field': 'singer',
            'title': '歌手名',
            'align': 'center'
        }, {
            'field': 'playCount',
            'title': '使用次数',
            'align': 'center'
        }]
    })
});

/**
 * 音乐模态框隐藏
 */
$('#musicModal').on('hidden.bs.modal',function(){
    $('#music_table').bootstrapTable('destroy')
});

/**
 * 复制或移动表单提交
 */
$('#copyBtn').click(function(){
    var tableData = $('#musicList_table').bootstrapTable('getSelections'),
        types = $('#musicType option:selected').val(),
        url,
        data = new Array();
    if($('input[name=types]:checked').val() == '1001'){
        url = '/community/musictype/copyMusicToAnotherType';
        $.each(tableData,function (index,item) {
            data.push(item.musicId)
        });
        var formData = {
            musicId: data,
            typeId: types
        }
    }else{
        url = '/community/musictype/cutMusicToAnotherType';
        $.each(tableData,function(index,item){
            data.push(item)
        });
        var formData = {
            musictyperel: data,
            typeId: types
        }
    }
    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(formData),
        contentType: 'application/json; charset=UTF-8',
        success: function (resp) {
            alert(resp.message);
        },
        error: function(xhr,textStatus,errorThrown){
            alert(textStatus)
        },
        complete: function(){
            $('#copyModal').modal('hide');
            $('#copyModal').on('hidden.bs.modal',function(){
                $('#musicList_table').bootstrapTable('refresh');
                $("#deleteMusic,#copyUser").attr("disabled", true);
            })
        }
    })
});


/**
 * 查询所有音乐分类
 */
$(function (){
   $.ajax({
       url: '/community/musictype/findMusictypeList',
       type: 'POST',
       dataType: 'json',
       data: {
            mark: true,
            offset: 0,
            limit: 100
       },
       success: function(resp){
           var data = resp.data.rows;
           $.each(data,function(index,item){
               $('#musicType').append("<option value='" +item.typeId+ "'>" +item.typeName+ "</option>")
           })
       },
       error: function (xhr,textStatus,errorThrown) {
           alert(textStatus)
       }
   })
});

/**
 * 添加用户表单验证
 */
$('#addForm').validationEngine('attach', {
    promptPosition: 'centerRight',
    scroll: false,
    binded: true
});

/**
 * 音乐推荐
 */
$('#recommend').click(function () {
    var tableData = $('#music_table').bootstrapTable('getSelections'),
        data = new Array();
    $.each(tableData,function (index,item) {
        data.push(item.musicId)
    });
    var params = {
        "musicId":data,
        "typeId":sessionStorage.getItem('musicType')
    };
    $.ajax({
        url:'/community/musictype/addMusicToTypeBatch',
        type:'POST',
        dataType:'json',
        data:JSON.stringify(params),
        contentType:'application/json; charset=UTF-8',
        success:function (resp) {
            alert(resp.message);
        },
        error:function (xhr,textStatus,errorThrown) {
            alert(textStatus)
        },
        complete: function(){
            $('#musicModal').modal('hide');
            $('#musicModal').on('hidden.bs.modal',function () {
                $('#musicList_table').bootstrapTable('refresh');
                $("#deleteMusic,#copyUser").attr("disabled", true);
            });
        }
    })
});

/**
 * 从分类批量移除音乐
 */
$('#deleteMusic').on('click',function () {
    return deleteMusic()
});

function deleteMusic() {
    var tableData = $('#musicList_table').bootstrapTable('getSelections'),
        data = new Array();
    $.each(tableData,function (index,item) {
        item['typeId'] = sessionStorage.getItem('musicType');
        data.push(item)
    })
    $.ajax({
        url: '/community/musictype/deleteMusicToTypeBatch',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify({"musictyperel":data}),
        success: function (resp) {
            alert(resp.message)
            $('#musicList_table').bootstrapTable('refresh')
        },
        error: function (xhr,textStatus,errorThrown) {
            alert(textStatus)
        }
    })
}

/**
 * 搜索表单提交
 */
$('#dataSearch').click(function () {
    return searchMusic($('#songName').val())
});

/**
 * 音乐搜索
 */
function searchMusic(name) {
    if($('#songName').val() == ''){
       $('#musicList_table').bootstrapTable('refreshOptions',{pageNumber: 1, pageSize: globalPageSize})
    }else{
        $.ajax({
            url: '/community/musictype/findTypeMusicByName',
            type: 'POST',
            dataType: 'json',
            data: {
                'name':name,
                'typeId':sessionStorage.getItem('musicType'),
                'offset':0,
                'limit':15
            },
            success:function (resp) {
                // alert(resp.message);
                $('#musicList_table').bootstrapTable('load',{
                    total:resp.data.total,
                    rows:resp.data.rows
                })
            },
            error:function (xhr,textStatus,errorThrown) {
                alert(textStatus)
            }
        });
    }
}




